﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Mrsoft.Common {
    /// <summary>
    /// 同文件存放的配置统一创建接口
    /// </summary>
    class ConfigFactory {
        /// <summary>
        /// XML文件全路径
        /// </summary>
        private readonly string file;

        public ConfigFactory(string file) {
            this.file = file;
        }

        public Config<T> GetConfig<T>(string confPath, T defValue) {
            return new Config<T>(file, confPath, defValue);
        }

        /// <summary>
        /// 存储添加了绑定内容项目
        /// </summary>
        private static HashSet<Control> controls = new HashSet<Control>();

        public static void BindingControlRegister(Control control) {
            if (!controls.Contains(control))
                controls.Add(control);
        }

        /// <summary>
        /// 将所有的数据绑定项目取消掉
        /// </summary>
        public static void ClearDataBindings() {
            lock (controls) {
                foreach (Control control in controls) {
                    control.DataBindings.Clear();
                }
                controls.Clear();
            }
        }
    }
}
